package cn.zhoutingrui.pethome.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class SendEmail {
    @Autowired
    private JavaMailSender javaMailSender;

    public void send2(String sendName,String emailName,String sentText,Boolean isHtml,String enclosureName,String enclosurePath,String addressee) throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(sendName);
        helper.setSubject(emailName);
        //第二个参数 true 代表是否是一个html内容
        helper.setText(sentText,isHtml);
        //添加附件
        helper.addAttachment(enclosureName,new File(enclosurePath));
        //helper.addAttachment("压缩文件", new File("C:\\resources.zip"));
        //收件人
        helper.setTo(addressee);
        //发送邮件
        javaMailSender.send(mimeMessage);}

    /**
     * 无附件发送
     * @param sendName
     * @param emailName
     * @param sentText
     * @param isHtml
     * @param addressee
     * @throws Exception
     */

    public void send2(String sendName,String emailName,String sentText,Boolean isHtml,String addressee) throws Exception{
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(sendName);
        helper.setSubject(emailName);
        //第二个参数 true 代表是否是一个html内容
        helper.setText(sentText,isHtml);
        //收件人
        helper.setTo(addressee);
        //发送邮件
        javaMailSender.send(mimeMessage);}
}
